$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'overload/list',
        datatype: "json",
        colModel: [			
			{ label: 'carryId', name: 'carryId', index: 'carry_id', width: 50, key: true },
			{ label: '行政区域', name: 'village', index: 'village', width: 80 }, 			
			{ label: '年度', name: 'year', index: 'year', width: 80 }, 			
			{ label: '经度', name: 'longitude', index: 'longitude', width: 80 }, 			
			{ label: '纬度', name: 'lat', index: 'lat', width: 80 }, 			
			{ label: '理论载畜量', name: 'theoryCarry', index: 'theory_carry', width: 80 }, 			
			{ label: '12月末', name: 'december', index: 'december', width: 80 }, 			
			{ label: '六月末', name: 'june', index: 'june', width: 80 }, 			
			{ label: '12月超载率', name: 'decOverload', index: 'dec_overload', width: 80 }, 			
			{ label: '六月超载率', name: 'juneOverload', index: 'june_overload', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		year:'',
        village:'',
		overload: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		yearquery: function (event) {
                			//input.focus();
                			//alert(vm.starttime);
                			 $("#jqGrid").jqGrid('setGridParam',{
                				 url:baseURL + 'overload/yearlist',
                				 postData:{
                					 'year':vm.year,
                					 'village':vm.village
                				 },
                				 datattype:'json',
                				 mtyp:'GET',
                				 viewrecords: true,
                			     height: 385,
                			     rowNum: 10,
                			     rowList : [10,30,50],
                			     rownumbers: true,
                			     rownumWidth: 25,
                			     autowidth:true,
                			     multiselect: true,
                			     pager: "#jqGridPager",
                			     jsonReader : {
                			         root: "page.list",
                			         page: "page.currPage",
                			         total: "page.totalPage",
                			         records: "page.totalCount"
                			     },
                			     prmNames : {
                			         page:"page",
                			         rows:"limit",
                			         order: "order"
                			     },
                			 }).trigger('reloadGrid');


        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.overload = {};
		},
		update: function (event) {
			var carryId = getSelectedRow();
			if(carryId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(carryId)
		},
		saveOrUpdate: function (event) {
			var url = vm.overload.carryId == null ? "overload/save" : "overload/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.overload),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var carryIds = getSelectedRows();
			if(carryIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "overload/delete",
                    contentType: "application/json",
				    data: JSON.stringify(carryIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(carryId){
			$.get(baseURL + "overload/info/"+carryId, function(r){
                vm.overload = r.overload;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});